package Data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import Domain.User;
import Main.Constants;

public class Usermapper extends Constants{

	/**
	 * @author Daniel Fagerberg
	 * 
	 * @param t
	 * @return 
	 * @throws SQLException
	 * 
	 */
	public User retrieve (String pwd) throws SQLException {
		
		User currentUser = new User();
		Statement stmt = null;
		String query = 
			"select user2.id_user" +
			"user2.password FROM user2 JOIN WHERE '::pwd' = password ";
		
		query = query.replaceAll("::pwd", "" + pwd);
		
		try {
			stmt = Database.createStatement();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		
		ResultSet rs = stmt.executeQuery(query);
		
		if (rs.next()) {
			currentUser.setId(rs.getInt("user_id"));
//			currentUser.setUsername(rs.getString("username"));
			currentUser.setPassword(rs.getString("password"));
			}
		else {
			JOptionPane.showMessageDialog(null, "Användaren finns ej i databasen", "felmeddelande", JOptionPane.ERROR_MESSAGE); 
		}
		
		return currentUser;
	}

}